package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.domain.BizUserAccount;
import cn.sfrog.project.biz.admin.domain.vo.BizUserAccountFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserAccountQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserAccountVO;
import cn.sfrog.project.biz.admin.mapper.BizUserAccountMapper;
import cn.sfrog.project.biz.admin.service.IBizUserAccountService;
import cn.sfrog.project.biz.web.vo.account.AppUserAccountResultVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 用户账户Service业务层处理
 */
@Service
public class BizUserAccountServiceImpl extends ServiceImpl<BizUserAccountMapper, BizUserAccount> implements IBizUserAccountService {
    @Resource
    private BizUserAccountMapper bizUserAccountMapper;

    /**
     * 查询用户账户
     *
     * @param id 用户账户主键
     * @return 用户账户
     */
    @Override
    public BizUserAccountVO selectBizUserAccountById(Long id) {
        return bizUserAccountMapper.selectBizUserAccountById(id);
    }

    /**
     * 查询用户账户列表
     *
     * @param query 用户账户
     * @return 用户账户
     */
    @Override
    public List<BizUserAccountVO> selectBizUserAccountList(BizUserAccountQueryVO query) {
        return bizUserAccountMapper.selectBizUserAccountList(query);
    }

    /**
     * 新增用户账户
     *
     * @param form 用户账户
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizUserAccount(BizUserAccountFormVO form) {
        BizUserAccount bizUserAccount = new BizUserAccount();
        BeanUtils.copyProperties(form, bizUserAccount);
        bizUserAccount.setCreateTime(new Date());
        bizUserAccount.setCreateBy(SecurityUtils.getUsername());
        bizUserAccount.setUpdateTime(new Date());
        bizUserAccount.setUpdateBy(SecurityUtils.getUsername());
        return bizUserAccount.insert();
    }

    /**
     * 修改用户账户
     *
     * @param form 用户账户
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizUserAccount(BizUserAccountFormVO form) {
        BizUserAccount bizUserAccount = new BizUserAccount();
        BeanUtils.copyProperties(form, bizUserAccount);
        bizUserAccount.setUpdateTime(new Date());
        bizUserAccount.setUpdateBy(SecurityUtils.getUsername());
        return bizUserAccount.updateById();
    }

    /**
     * 批量删除用户账户
     *
     * @param ids 需要删除的用户账户主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizUserAccountByIds(Long[] ids) {
            return BizUserAccount.DAO.delete(Wrappers.lambdaQuery(BizUserAccount.class).in(BizUserAccount::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
//        return BizUserAccount.DAO.update(Wrappers.lambdaUpdate(BizUserAccount.class).in(BizUserAccount::getId, (Object[]) vo.getIds()).set(BizUserAccount::getStatus, vo.getStatus()));
        return true;
    }

    @Override
    public BigDecimal selectAllUserAccountAmount() {
        return bizUserAccountMapper.selectAllUserAccountAmount();
    }

    @Override
    public AppUserAccountResultVO selectAppMyAccount(Long userId) {
        return bizUserAccountMapper.selectAppMyAccount(userId);
    }

    @Override
    public Boolean createUserAccount(Long userId) {
        BizUserAccount bizUserAccount = new BizUserAccount();
        bizUserAccount.setUserId(userId);
        return bizUserAccount.insert();
    }
}
